package com.huang;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class CS {
     static List<Integer> list = new ArrayList<>(5);//

    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();

        //消费者
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (int i1 = 0; i1 < 100; i1++) {
                    synchronized (o) {
                        while (list.size()==0){
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        list.remove(0);
                        System.out.println(Thread.currentThread().getName()+"消费了");
                        o.notifyAll();
                    }
                }


            }).start();
        }


        //生产者
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (int i1 = 0; i1 < 100; i1++) {
                    synchronized (o) {
                        while (list.size()==5){
                            try {
                                o.wait();

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        list.add(1);
                        System.out.println(Thread.currentThread().getName()+"sc");
                        o.notifyAll();

                    }
                }
            }).start();
        }












    }
}
